package com.ruoyi.asset.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AstSite;
import com.ruoyi.asset.service.IAstSiteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 站点管理Controller
 * 
 * @author xunzhe
 * @date 2024-06-11
 */
@Controller
@RequestMapping("/asset/astSite")
public class AstSiteController extends BaseController
{
    private String prefix = "asset/astSite";

    @Autowired
    private IAstSiteService astSiteService;

    @RequiresPermissions("asset:astSite:view")
    @GetMapping()
    public String astSite()
    {
        return prefix + "/astSite";
    }

    /**
     * 查询站点管理树列表
     */
    @RequiresPermissions("asset:astSite:list")
    @PostMapping("/list")
    @ResponseBody
    public List<AstSite> list(AstSite astSite)
    {
        List<AstSite> list = astSiteService.selectAstSiteList(astSite);
        return list;
    }

    /**
     * 导出站点管理列表
     */
    @RequiresPermissions("asset:astSite:export")
    @Log(title = "站点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AstSite astSite)
    {
        List<AstSite> list = astSiteService.selectAstSiteList(astSite);
        ExcelUtil<AstSite> util = new ExcelUtil<AstSite>(AstSite.class);
        return util.exportExcel(list, "站点管理数据");
    }

    /**
     * 新增站点管理
     */
    @GetMapping(value = { "/add/{siteId}", "/add/" })
    public String add(@PathVariable(value = "siteId", required = false) Long siteId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(siteId))
        {
            mmap.put("astSite", astSiteService.selectAstSiteBySiteId(siteId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存站点管理
     */
    @RequiresPermissions("asset:astSite:add")
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AstSite astSite)
    {
        return toAjax(astSiteService.insertAstSite(astSite));
    }

    /**
     * 修改站点管理
     */
    @RequiresPermissions("asset:astSite:edit")
    @GetMapping("/edit/{siteId}")
    public String edit(@PathVariable("siteId") Long siteId, ModelMap mmap)
    {
        AstSite astSite = astSiteService.selectAstSiteBySiteId(siteId);
        mmap.put("astSite", astSite);
        return prefix + "/edit";
    }

    /**
     * 修改保存站点管理
     */
    @RequiresPermissions("asset:astSite:edit")
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AstSite astSite)
    {
        return toAjax(astSiteService.updateAstSite(astSite));
    }

    /**
     * 删除
     */
    @RequiresPermissions("asset:astSite:remove")
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{siteId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("siteId") Long siteId)
    {
        int i = astSiteService.selectCount(siteId);
        if(i > 0){
            return AjaxResult.warn("存在下级站点,不允许删除");
        }
        return toAjax(astSiteService.deleteAstSiteBySiteId(siteId));
    }

    /**
     * 选择站点管理树
     */
    @GetMapping(value = { "/selectAstSiteTree/{siteId}", "/selectAstSiteTree/" })
    public String selectAstSiteTree(@PathVariable(value = "siteId", required = false) Long siteId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(siteId))
        {
            mmap.put("astSite", astSiteService.selectAstSiteBySiteId(siteId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载站点管理树列表
     */
    @GetMapping(value = { "/treeData/{taskId}", "/treeData" })
    @ResponseBody
    public List<Ztree> treeData(@PathVariable(value = "taskId", required = false) Long taskId)
    {
        List<Ztree> ztrees = astSiteService.selectAstSiteTree(taskId);
        return ztrees;
    }
}
